/*
Dominique Jannone
Guillaume Marre
Yohan Ojeda
Laurent Schiavon
*/
package model;

import vue.Fenetre;
import controleur.Constantes;
import Econf_Audio.Emetteur_client;
import Econf_Audio.Receveur_client;
import java.util.logging.Logger;
import java.util.logging.Level;

public class Conference extends Thread {

	private eConf eConf;
	private String roleUser;
	private Fenetre fenetre;
	private int oneShoot;
	private boolean finConference;
	private Integer portLocalR;
	private Integer portE;
	private String ipServeur;
	private Receveur_client receveur;
	private Emetteur_client emetteur;
	
	//log
	public static final Logger logger = Logger.getLogger(Conference.class.getName());

	public Conference(eConf eConf, String ipServeur) {
		// TODO Auto-generated constructor stub
		this.eConf = eConf;
		this.ipServeur = ipServeur;
		fenetre = eConf.getFenetre();
		oneShoot=0;
		finConference=false;
		
	}
	
	// Observateurs
	public Receveur_client getReceveur() {
		return receveur;
	}
	public Emetteur_client getEmetteur() {
		return emetteur;
	}
	public boolean getFinConference() {
		return finConference;
	}
	
	public void setFinConference(boolean finConference) {
		this.finConference = finConference;
	}

	public void setEmetteur(Emetteur_client emetteur) {
		this.emetteur = emetteur;
	}

	@Override
	public void run() {
		// TODO Auto-generated method stub
		 logger.log(Level.INFO,"Thread Conference");
		while (!finConference){
			while(eConf.isConferenceOnAir() & !finConference){
				try {
					Thread.sleep(1000);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					logger.log(Level.SEVERE,e.getClass() + ": " +  e.getMessage(), e);
				}
				
				Object[] tabInfoPortLocal = eConf.RecoiReponseDuServeur();
				portLocalR =  new Integer(tabInfoPortLocal[0].toString());
				portE =  new Integer(tabInfoPortLocal[1].toString());
				// Update eConf sur le portLocalR
				eConf.setPortEmetteurDuServeur(portLocalR);
				logger.log(Level.INFO,"Info PortEmmision et portLocalRec = "+portE+"-"+portLocalR);
				
				
				logger.log(Level.INFO,"Dans le Thread Conference >>> isConferenceOnAir = "+eConf.isConferenceOnAir());
				this.roleUser = eConf.getRoleUserConferenceActuel();
				// Executer une seul fois:
				if (oneShoot==0){
					logger.log(Level.INFO,"Dans le Thread Conference >>> oneShoot ");
					receveur = new Receveur_client(portE);
					receveur.start();
					if(roleUser.equals(Constantes.ROLE_CONFERENCIER_STRING)){
						logger.log(Level.INFO,"Lancement de l'Emetteur_client");
						emetteur = new Emetteur_client(portLocalR, ipServeur);
						emetteur.start();
					}
					oneShoot++;
				}
						
				finConference = true;	
			}
			
			
			// pause
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				logger.log(Level.SEVERE,e.getClass() + ": " +  e.getMessage(), e);
			}
			
			
			
			
		}
		
		
	}



}
